package com.polaris.live.ui.follow

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.adapter.FollowAdapter
import com.polaris.live.databinding.FragmentFollowingBinding
import com.polaris.live.viewmodel.FollowViewModel
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.translation.R.string
import com.polaris.live.R
import com.polaris.live.constant.UserConst
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router

/**
 * FollowingFragment
 *
 * @author Created by 半仙 on 2023/9/4/004 14:32
 */
class FollowingFragment : BaseVbFragment<FollowViewModel, FragmentFollowingBinding>() {

    private val mAdapter: FollowAdapter by lazy {
        FollowAdapter()
    }

    override fun initView(savedInstanceState: Bundle?) {
        initObsever()

        mBinding.recyclerView.setEnableRefresh(false)
        mBinding.recyclerView.setEmptyTip(R.drawable.ic_focus_empty, getString(string.no_focus))
        mBinding.recyclerView.apply {
            recycler.layoutManager = GridLayoutManager(context, 3, RecyclerView.VERTICAL, false)
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@FollowingFragment,
                mViewModel.followersLiveData
            )
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.data[position]

            Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                .withInt("page", UserConst.From.MY)
                .withLong("userId", item.userId)
                .withAny("userBasic", item)
                .navigation(requireActivity())
        }

        mViewModel.onRefresh()
    }

    private fun initObsever() {
        mViewModel.followersLiveData.observe(this) { resultState ->
            parseState(resultState, {

            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

}